/**
 * Created by Administrator on 2017/12/14.
 */
angular.module('module.order')
    .controller('OrderController', OrderController);
OrderController.$inject = ['$state','OrderService', '$stateParams'];
function OrderController($state,OrderService, $stateParams) {
    var vm = this;
    vm.tableId = $stateParams.stid;
    console.log(vm.tableId);
    /*根据当前时间自动生成订单编号*/
    vm.timestamp = (new Date()).valueOf();
    /*计算订单部分 显示菜品的高度*/
    var HEIGHT = $('body').height();
    $(window).resize(function () {
        $('.eshop').height(HEIGHT);
    });
    /*计算高度*/
    var  _height =$("#content").height();
    var  _top =$("#top").height();
    var  _bottom =$("#bottom").height();
    $("#center").height(_height-_top-_bottom);

    /*点击菜品 将菜品加入虚拟购物车*/
    vm.addOrderlist = addOrderlist;
    /*删除所选商品*/
    vm.removeGood = removeGood;
    /*点击显示删除键*/
    vm.showRemove = showRemove;
    /*返回上一页*/
    vm.goback = goback;
    vm.orderInfo = {};
    vm.ordergoods = [];
    vm.placeOrder = placeOrder;/*下单*/
    vm.shopid = 977534855;
    /*获取菜品分类*/
    OrderService.getGood(vm.shopid).$promise.then(function (data) {
        vm.menusList = data;
        changeWidth();
    });
    /*读取该桌台的订单*/
    OrderService.orderDetail(vm.shopid, vm.tableId,1,1).$promise.then(
        function(data){
            console.log(data);
            vm.ordergoods = data[0].ordersShops[0].ordersShopsGoods/*.push( vm.ordergoods)*/;
          console.log(
              data[0].payment
          );
             vm.price= data[0].payment;
        },function(error){
            console.log(error);
        }
    )

    /*点击菜品 将菜品加入虚拟购物车*/
 /*   vm.price = 0;*/
    function addOrderlist(good) {
        vm.price +=good.price;
        var index = vm.ordergoods.indexOf(good);
        if (index > -1) {
            good.num++;
        } else {
            good.num = 1;
            vm.ordergoods.push(good);
        }
    }
    function showRemove() {
        vm.tr = !vm.tr;
    }
    /*删除所选商品*/
    function removeGood(goods,index) {
        vm.ordergoods.splice(index, 1);
        //价格计算
        goods[index].price * goods[index].num
    }
    function goback() {
        console.log(vm.ordergoods.length);
        if (vm.ordergoods.length == 0) {
            $state.go("table");
        }
        else {

        }
    }//.....

    /*下单*/
   function placeOrder(){
       //
       vm.orderInfo = {
           "creatorid": 5,
           "type": 0,
           "ordersShops": [
               {
                   "shopid": vm.shopid,
                   "osid": vm.tableId,
                   "pnum": 0,
                   "ordersShopsGoods": [
                   ]
               }
           ]
       };
       for(var i=0;i<vm.ordergoods.length;i++){
           var cartGoods = vm.ordergoods[i];
           var goods = {
               "goodsid": cartGoods.goodsid,
               "skuid": 0,
               "fee": cartGoods.price,
               "currentFee": cartGoods.price,
               "num": cartGoods.num,
               "remarks": ""
           };
           vm.orderInfo.ordersShops[0].ordersShopsGoods.push(goods)
       }
         /* return;*/
       OrderService.order(vm.orderInfo).$promise.then(
           function(data){
               console.log("成功");
               vm.show = true;
           },function(error){
               alert("下单失败")
           })
   }
    /*随着li的增加 ul的宽度增加   顶部菜单分类*/
    var shopMenu = $("#menu-fenlei");
    function changeWidth(menu) {
        var liList = shopMenu.find("li");
        if (liList.length == 0) {
            setTimeout(function () {
                changeWidth(menu)
            }, 10)
        } else {
            var _width = 0;
            for(var i = 0; i < liList.length; i++){
                _width += liList[i].offsetWidth+2;
            }
            shopMenu.width(_width);
        }
    }
}